<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button v-permission="['超级管理员','管理员']" type="primary" icon="el-icon-plus" size="mini" @click="dialogFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="模块" value="1"></el-option>
              <el-option label="等级" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 设备归属管理table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="140" align="center" prop="create_time" label="更新日期" sortable>
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="status" label="重要程度">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.grade" disabled>
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="130" align="center" prop="modular" label="优化模块"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="130" align="center" prop="describe" label="优化详情"></el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <!-- <el-button size="mini" type="primary" icon="el-icon-edit" @click="imgShow(scope.row.address)">查看</el-button> -->
            <el-button size="mini" type="primary" @click="imgShow(scope.row.address)">下载</el-button>
            <el-button size="mini" v-permission="['超级管理员','管理员']" @click="getdeleteNotice(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination background :current-page.sync="listQuery.startSize" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加 -->
    <el-dialog title="系统更新消息推送" :visible.sync="dialogFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <el-form :model="addForm" :rules="rules" ref="ruleForm">
        <el-form-item label="重要程度" label-width="100px" prop="grade">
          <div style="height:40px" class="flex felx_align setting">
            <el-rate v-model="addForm.grade"></el-rate>
          </div>
        </el-form-item>
        <el-form-item label="模块" label-width="100px" prop="modular">
          <el-input v-model="addForm.modular" class="add_select" placeholder="请输入模块"></el-input>
        </el-form-item>
        <el-form-item label="优化说明" label-width="100px" prop="describe">
          <el-input v-model="addForm.describe" class="add_select" placeholder="请输入优化说明" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="图片上传" label-width="100px" class="snElsx" prop="address">
          <div class="addPut">
            <el-input autocomplete="off" placeholder="请点击上传，上传pdf文件"  v-model="addForm.address"></el-input>
            <el-upload class="upload-demo dlePut btnLocation"  action :auto-upload="false" :show-file-list="false" :on-change="compileapkAvatarUpload">
              <el-button round size="mini">上传</el-button>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <Debounce :time='500' :isDebounce="3">
        <el-button type="primary" @click.native="getaddNotice('ruleForm')" size="mini" round>确 定</el-button>
        </Debounce>
      </div>
    </el-dialog>
    <!-- 图片查看 -->
    <el-dialog title="　" :visible.sync="imgdialogVisible" style="border:none">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryNotice, noticeImg, addNotice, deleteNotice } from "@/api/system.js";
import permission from '@/directive/permission/index.js' // 权限判断指令
import deleteDialog from "@/components/deleteDialog";
export default {
  directives: { permission },
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["更新日期", "重要程度", "优化模块", "优化详情"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "", //搜索条件
      dialogFormVisible: false,
      //添加表单
      addForm: { grade: 1, modular: "", describe: "", address: "" },
      rules: {
        modular: [{ required: true, message: "请输入模块", trigger: "blur" }],
        describe: [{ required: true, message: "请输入优化说明", trigger: "blur" }]
      },
      //图片查看
      imgdialogVisible: false,
      dialogImageUrl: '',
      //删除弹窗
      deleteDialogs: { show: false, deleteText: '', deleteId: 0 },
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), type: 1, modular: undefined, grade: undefined }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryNotice()
  },
  methods: {
    //删除
    getdeleteNotice(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), id: this.deleteDialogs.deleteId }
      deleteNotice(params).then(res => {
        this.$message.success(res.msg)
        this.getqueryNotice()
      })
    },
    imgShow(img) {
      // this.imgdialogVisible = true
      // this.dialogImageUrl = img
      window.open(img)
    },
    //添加更新日志
    getaddNotice(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.userId = localStorage.getItem("ID")
          addNotice(addForm).then(res => {
            this.$message.success(res.msg)
            this.getqueryNotice()
            this.dialogFormVisible = false
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    getqueryNotice(startSize = 1, pageSize = 10, type = 1, parameter) {
      let qs = {
        startSize: startSize,
        pageSize: pageSize,
        type: type,
        userId: localStorage.getItem("ID"),
        ...parameter
      };
      queryNotice(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
        console.log('成功',res.data)
      }).catch(err => {
        console.log('请求失败', err)
        this.browserTable = []
      })
    },
    //图片上传
    compileapkAvatarUpload(file) {
      var fd = new FormData()
      fd.append('file', file.raw)
      fd.append('last_ip', sessionStorage.getItem("Ip"))
      fd.append('userId', localStorage.getItem("ID"))
      noticeImg(fd).then(res => {
        this.addForm.address = res.data.imgPath
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryNotice();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryNotice();
    },
    //搜索,1:模块:等级
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.type = 2
          this.listQuery.modular = this.searchTable
          this.getqueryNotice();
          break;
        case '2':
          this.listQuery.type = 3
          this.listQuery.grade = this.searchTable
          this.getqueryNotice();
          break;
      }
    },
    //编辑添加表单关闭事件
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.addForm = { grade: 1, modular: "", describe: "", address: "" }
      })
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), type: 1, modular: undefined, grade: undefined }
      this.getqueryNotice()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  },
}
</script>